class Solution
{
public:
    class BinaryTrie
    {
    public:
        BinaryTrie()
        {
            left = nullptr;
            right = nullptr;
        }

        BinaryTrie *left;
        BinaryTrie *right;
    };

    int findMaximumXOR(vector<int> &nums)
    {
        BinaryTrie *root = new BinaryTrie();
        int result = 0;
        for (int num : nums)
        {
            BinaryTrie *node = root;

            for (int i = 31; i >= 0; --i)
            {
                int bit = (num >> i) & 1;

                if (bit == 0)
                {
                    if (node->left == nullptr)
                    {
                        node->left = new BinaryTrie();
                    }
                    node = node->left;
                }
                else
                {
                    if (node->right == nullptr)
                    {
                        node->right = new BinaryTrie();
                    }
                    node = node->right;
                }
            }
        }

        for (int num : nums)
        {
            int maxXor = 0;
            BinaryTrie *node = root;
            for (int i = 31; i >= 0; --i)
            {
                int bit = (num >> i) & 1;
                if (bit == 0)
                {
                    if (node->right)
                    {
                        maxXor += (1 << i);
                        node = node->right;
                    }
                    else
                    {
                        node = node->left;
                    }
                }
                else
                {
                    if (node->left)
                    {
                        maxXor += (1 << i);
                        node = node->left;
                    }
                    else
                    {
                        node = node->right;
                    }
                }
            }
            result = max(result, maxXor);
        }
        return result;
    }
};